package package01.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * 作者: jack
 * 时间: 2020-07-21 09:22
 * 描述: ObjectEncoder
 * 对象序列化
 */
public class ObjectEncoder {

    public static byte[] obj2byte(Object obj) {
        ObjectOutputStream out = null;
        ByteArrayOutputStream bo = null;
        try {
            bo = new ByteArrayOutputStream();
            // 将对象保存到字节数据流
            out = new ObjectOutputStream(bo);
            out.writeObject(obj);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bo.toByteArray();
    }

}
